﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Examination.BusinessLogicLayer;

public partial class Web_CompositionManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string loginName = Session["userID"].ToString();
            Users user = new Users();
            user.LoadData(loginName);
            labUser.Text = user.UserName;
            InitDDLData();  //初始化试卷科目          
        }
    }
    //初始化试卷科目
    protected void InitDDLData()
    {
        Course course = new Course();       //创建试卷科目对象
        DataSet ds = course.QueryCourse();  //查询试卷科目信息
        GridView1.AllowPaging = true;//是否启用分页
        GridView1.PageSize = 8;//每页显示条数
        ddlCourse.DataSource = ds;          //指名试卷科目列表框数据源
        ddlCourse.DataTextField = "Name";   //DataTextField显示Name字段值
        ddlCourse.DataValueField = "ID";    //DataValueField显示ID字段值
        ddlCourse.DataBind();               //绑定数据
        GridViewBindByCourse();

        //如果当前索引为0 则 禁用 首页 上一页 按钮
        if (GridView1.PageIndex == 0)
        {
            this.LinkButton1.Enabled = false;
            this.LinkButton2.Enabled = false;
        }
        else
        {
            //否则开启按钮
            this.LinkButton1.Enabled = true;
            this.LinkButton2.Enabled = true;
        }
        //如果当前索引为-1 则 禁用 下一页 尾页 按钮 按钮
        if (GridView1.PageIndex == GridView1.PageCount - 1)
        {
            this.LinkButton3.Enabled = false;
            this.LinkButton4.Enabled = false;
        }
        else
        {
            //否则开启按钮
            this.LinkButton3.Enabled = true;
            this.LinkButton4.Enabled = true;
        }

    }


    //显示选择科目的综合题
    protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewBindByCourse();//为GridView绑定数据
    }

    protected void GridViewBindByCourse()
    {
        CompositionProblem compositionproblem = new CompositionProblem();  //创建多选题对象
        DataSet ds = compositionproblem.QueryCompositionProblem(int.Parse(ddlCourse.SelectedValue));//根据试卷科目查询多选题信息
        GridView1.DataSource = ds.Tables[0].DefaultView;    //为GridView控件指名数据源
        GridView1.DataBind();                               //绑定数据
    }

    //删除试题事件
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        CompositionProblem compositionproblem = new CompositionProblem();  //创建单选题对象
        int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); //取出要删除记录的主键值
        if (compositionproblem.DeleteByProc(ID))
        {
            Response.Write("<script language=javascript>alert('成功删除试题！')</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('删除试题失败！')</script>");
        }
        GridViewBindByCourse();//重新绑定数据
    }
    //删除多条记录
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        CompositionProblem multi = new CompositionProblem();//创建CompositionProblem对象              
        foreach (GridViewRow dr in GridView1.Rows)//对GridView中的每一行进行判断
        {
            if (((CheckBox)dr.FindControl("xuanze")).Checked)//如果选择了进行删除
            {
                int ID = int.Parse(((Label)dr.FindControl("Label1")).Text);
                multi.ID = ID;
                multi.DeleteByProc(ID);
            }
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridViewBindByCourse();//重新绑定数据
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].ToolTip = e.Row.Cells[2].Text;
            if ((e.Row.Cells[2].Text).Length > 30)
            {
                e.Row.Cells[2].Text = (e.Row.Cells[2].Text).Substring(0, 30) + "...";
            }
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((ImageButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除\"" + e.Row.Cells[2].Text + "\"吗?')");
            }

        }
        int i;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E4ECFB'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        switch (((LinkButton)sender).CommandArgument.ToString())
        {
            //将不同CommandArgument参数  而同一CommandName名字的4个按钮 循环取CommandArgument参数 利用索引进行判断
            //从而实现分页功能 注意的一点是 4个 按钮必须要触发同一个  单击事件
            case "first":
                GridView1.PageIndex = 0;
                break;
            case "last":
                GridView1.PageIndex = GridView1.PageCount - 1;
                break;
            case "prev":
                GridView1.PageIndex = GridView1.PageIndex - 1;
                break;
            case "next":
                GridView1.PageIndex = GridView1.PageIndex + 1;
                break;

        }
        //之后 重新绑定
        InitDDLData();
    }
}
